package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;

/**
 * Created by yanggeng on 2017/7/6.
 */

public class MineActivity extends AppCompatActivity {

    private WebView webView;
    private ImageView iv_mine;
    private Toolbar toolbar;
    private AppBarLayout appBarLayout;
    private TextView tv_title;
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        context = this;
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        tv_title = (TextView) findViewById(R.id.tv_title);
        findViewById(R.id.iv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        toolbar.setTitle("个人中心");
//        toolbar.setNavigationIcon(R.mipmap.type_checked);
        setSupportActionBar(toolbar);

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                double offset = Math.abs(verticalOffset);
                int scrollRange = appBarLayout.getTotalScrollRange();
                float alpha = 1f;
                if(verticalOffset == 0) {//展开状态
                    tv_title.setText("国寿i车");
                } else if(offset < scrollRange) {
                    if(offset < scrollRange / 2) { // 折叠过程前半段
                        alpha = 1f - (float) offset / (scrollRange / 2);
                        tv_title.setAlpha(alpha);
                        if(!tv_title.getText().equals("国寿i车")){
                            tv_title.setText("国寿i车");
                        }
                    } else { //折叠过程后半段
                        alpha = 1f - (float) (scrollRange - offset) / (scrollRange / 2);
                        tv_title.setAlpha(alpha);
                        if(!tv_title.getText().equals("个人中心")){
                            tv_title.setText("个人中心");
                        }
                    }
                } else { //折叠状态
                    tv_title.setText("个人中心");
                }
            }
        });

        iv_mine = (ImageView) findViewById(R.id.iv_mine);
        iv_mine.setImageResource(R.mipmap.ic_launcher);
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.baidu.com");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
